package com.wxw.BaseLibrary.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import com.wxw.BaseLibrary.R;
import com.wxw.BaseLibrary.tool.ActivityTool;
import com.wxw.BaseLibrary.tool.ExitApplication;

import java.util.ArrayList;
import java.util.List;

public abstract class FragmentTabhostActivity extends FragmentActivity {
    protected FragmentTabHost mTabHost;
    protected LayoutInflater mLayoutInflater;
    protected List<FragmentData> fragmentDataList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityTool.fullScreen(this);
        ActivityTool.hideTitle(this);
        ExitApplication.getInstance().addActivity(this);
        mLayoutInflater = LayoutInflater.from(this);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

        initView();
    }

    /**
     * 设置Activity的Layout ID，并初始化TabHost对象
     */
    public abstract void initView();

    protected void setupTabHost(int backgroundID, int layoutId, int imgViewId, int textViewId) {
        int i = 0;
        for (FragmentData data: fragmentDataList) {
            // 给每个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(data.getText())
                    .setIndicator(getTabSpecView(data, layoutId, imgViewId, textViewId));
            // 将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, data.getFragmentClass(), null);
            // 设置Tab按钮的背景
            mTabHost.getTabWidget().getChildAt(i++)
                    .setBackgroundResource(backgroundID);
        }
    }

    protected View getTabSpecView(FragmentData data, int layoutId, int imgViewId, int textViewId) {
        View view = mLayoutInflater.inflate(layoutId, null);
        ImageView imageView = (ImageView) view.findViewById(imgViewId);
        imageView.setImageResource(data.getImageID());
        TextView textView = (TextView) view.findViewById(textViewId);
        textView.setText(data.getText());
        return view;
    }

    public class FragmentData {
        private Class<Fragment> fragmentClass;
        private int imageID;
        private String text;

        public FragmentData(Class cls, int imageID, String text) {
            this.fragmentClass = cls;
            this.imageID = imageID;
            this.text = text;
        }

        public Class<Fragment> getFragmentClass() {
            return fragmentClass;
        }

        public void setFragmentClass(Class<Fragment> fragmentClass) {
            this.fragmentClass = fragmentClass;
        }

        public int getImageID() {
            return imageID;
        }

        public void setImageID(int imageID) {
            this.imageID = imageID;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }
}
